package cz.idsv.web.mvc.model.dao;

import java.util.Collection;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.idsv.web.mvc.model.entity.ProductTypeEntity;


/**
 * ProductTyp DAO implementation.
 * 
 * @author Jan Borzensky
 */

@Repository("ProductTypeDaoImpl")
@Transactional(readOnly = true)
public class ProductTypeDaoImpl implements ProductTypeDao {

	protected HibernateTemplate template = null;

	/**
	 * Sets Hibernate session factory.
	 */
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		template = new HibernateTemplate(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	public Collection<ProductTypeEntity> findProductType() throws DataAccessException {
		return template.find("from ProductType");
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void save(ProductTypeEntity productTypeEntity) {
		template.saveOrUpdate(productTypeEntity);
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void delete(ProductTypeEntity productTypeEntity) {
		template.delete(productTypeEntity);
	}

	public ProductTypeEntity findProductTypeById(Integer id) throws DataAccessException{
		return template.get(ProductTypeEntity.class, id);
	}

}
